
package com.be.beadmin.device.impl;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.entity.InspectSubItem;
import com.be.beadmin.device.entity.InspectSubItem_;
import cn.hutool.core.util.ObjectUtil;

import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.device.mapper.InspectSubItemMapperEx;
import com.be.beadmin.device.repository.InspectSubItemRepository;
import com.be.beadmin.device.service.InspectSubItemService;
import com.be.beadmin.device.dto.input.InspectSubItemDto;
import com.be.beadmin.device.dto.output.InspectSubItemEx;
import com.be.beadmin.device.dto.input.InspectSubItemQueryCriteria;
import com.be.beadmin.device.mapper.InspectSubItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author chenxu
* @date 2023-10-06
**/
@Service
@RequiredArgsConstructor
public class InspectSubItemServiceImpl implements InspectSubItemService {

    @Autowired
    private  InspectSubItemRepository inspectSubItemRepository;
    @Autowired
    private  InspectSubItemMapper inspectSubItemMapper;
    @Autowired
    private  InspectSubItemMapperEx inspectSubItemExMapper;

    @Override
    public Page<InspectSubItemEx> getInspectSubItemByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<InspectSubItem> page = inspectSubItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(InspectSubItem_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(InspectSubItem_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<InspectSubItemEx> page1 = page.map(inspectSubItemExMapper::toEntity);
            return page1;

    }

    @Override
    public void addInspectSubItem(InspectSubItemDto inspectSubItemDto) {
            InspectSubItem result = inspectSubItemRepository.findInspectSubItemByIdAndDelete(inspectSubItemDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("InspectSubItem不存在,请重新查询");
            }
            InspectSubItem inspectSubItem = inspectSubItemMapper.toEntity(inspectSubItemDto);
            inspectSubItemRepository.save(inspectSubItem);
    }

    @Override
    public void updateInspectSubItem(InspectSubItemDto inspectSubItemDto) {
            InspectSubItem inspectSubItem = inspectSubItemRepository.findInspectSubItemById(inspectSubItemDto.getId());
            inspectSubItem = inspectSubItemMapper.toEntity(inspectSubItemDto);
            inspectSubItemRepository.save(inspectSubItem);
    }


    @Override
    public void delInspectSubItem(String id) {
        InspectSubItem inspectSubItem = inspectSubItemRepository.findInspectSubItemById(id);
        inspectSubItem.setDelete(1);
        inspectSubItemRepository.save(inspectSubItem);
    }

    @Override
    public void delInspectSubItems (List<String> ids) { }
}